/* eslint-disable */
const isProduction = process.env.NODE_ENV === 'production'
const isDevelopment = process.env.NODE_ENV === 'development'
const DEPLOY_ENV = process.env.DEPLOY_ENV
const isDeploy = DEPLOY_ENV === 'GH_PAGES'
const dotenv = require('dotenv').config({
  path: isDevelopment ? '.env' : isDeploy ? '.env.generate' : '.env.production'
})
const {
  APP_URL,
  PUBLIC_PATH,
  AXIOS_BASEURL,
  AXIOS_PREFIX,
  API_SERVER,
  API_PREFIX,
  ROUTER_BASE,
  DEST_DIR
} = dotenv.parsed
const routerBase = isDeploy
  ? {
      base: `/${ROUTER_BASE}/`
    }
  : {}

export default {
  env: {
    isDevelopment,
    isProduction,
    APP_URL,
    PUBLIC_PATH,
    AXIOS_BASEURL,
    AXIOS_PREFIX,
    API_SERVER,
    API_PREFIX,
    ROUTER_BASE,
    DEST_DIR,
    DEPLOY_ENV,
    NODE_ENV: process.env.NODE_ENV
  },
  srcDir: 'src/',
  // buildDir: 'nuxt-dist',
  /*
   ** Nuxt rendering mode
   ** See https://nuxtjs.org/api/configuration-mode
   */
  mode: 'universal',
  /*
   ** Nuxt target
   ** See https://nuxtjs.org/api/configuration-target
   */
  target: 'server',
  /*
   ** Headers of the page
   ** See https://nuxtjs.org/api/configuration-head
   */
  head: {
    title: process.env.npm_package_name || '',
    meta: [
      { charset: 'utf-8' },
      { name: 'viewport', content: 'width=device-width, initial-scale=1' },
      {
        hid: 'description',
        name: 'description',
        content: process.env.npm_package_description || ''
      }
    ],
    link: [
      {
        rel: 'icon',
        type: 'image/x-icon',
        href: './favicon.ico'
      }
    ]
  },
  /*
   ** Global CSS
   */
  css: ['element-ui/lib/theme-chalk/index.css'],
  /*
   ** Plugins to load before mounting the App
   ** https://nuxtjs.org/guide/plugins
   */
  plugins: [
    '@/plugins/element-ui',
    '@/plugins/echarts',
    { src: '@/plugins/globalFilter.js', ssr: true },
    '@/plugins/axios',
    { src: '@/plugins/loading', ssr: false }
  ],
  /*
   ** Auto import components
   ** See https://nuxtjs.org/api/configuration-components
   */
  components: true,
  /*
   ** Nuxt.js dev-modules
   */
  buildModules: [
    // Doc: https://github.com/nuxt-community/eslint-module
    '@nuxtjs/eslint-module',
    // Doc: https://github.com/nuxt-community/stylelint-module
    '@nuxtjs/stylelint-module'
  ],
  /**
   * router extra configurations
   */
  router: {
    middleware: ['auth'],
    ...routerBase
  },
  /*
   ** Nuxt.js modules
   */
  modules: [
    '@nuxtjs/axios',
    '@nuxtjs/auth',
    ['@nuxtjs/dotenv', { path: __dirname }]
  ],
  axios: {
    prefix: AXIOS_PREFIX,
    proxy: isDevelopment,
    baseURL: AXIOS_BASEURL
  },
  auth: {
    strategies: {
      local: {
        endpoints: {
          login: {
            url: DEPLOY_ENV || isDevelopment ? '/login.json' : '/login',
            method: 'get',
            propertyName: 'token'
          },
          logout: {
            url: DEPLOY_ENV || isDevelopment ? '/logout.json' : '/logout',
            method: 'get'
          },
          user: {
            url: DEPLOY_ENV || isDevelopment ? '/user.json' : '/user',
            method: 'get',
            propertyName: 'user'
          }
        },
        tokenRequired: true,
        tokenType: false,
        autoFetchUser: true
      }
    },
    redirect: {
      login: '/login',
      logout: '/',
      callback: '/login',
      home: '/'
    },
    watchLoggedIn: true,
    token: {
      prefix: '_token.'
    }
  },
  proxy: {
    ['/' + API_PREFIX]: {
      target: API_SERVER,
      pathRewrite: { ['^/' + API_PREFIX]: '/' }
    }
  },

  /*
   ** Build configuration
   ** See https://nuxtjs.org/api/configuration-build/
   */
  build: {
    transpile: [/^element-ui/]
  },
  generate: {
    dir: DEST_DIR,
    subFolders: false
  }
}
